package com.shuyun.xxx.guava.offer;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Created by feng.ding on 2017/5/21.
 */
public class DogAndCat {

    private Pet pet;
    private long count;
    public DogAndCat(Pet pet, long count){
        this.pet = pet;
        this.count = count;
    }
    public Pet getPet(){
        return pet;
    }
    public long getCount(){
        return count;
    }
    public String getPetType(){
        return this.pet.getType();
    }

    private static class DogCatQueue{
        private Queue<DogAndCat> dogQ;
        private Queue<DogAndCat> catQ;
        private long count;
        public DogCatQueue(){
            dogQ = new LinkedList<>();
            catQ = new LinkedList<>();
            this.count = 0;
        }
        public void add(Pet pet){
            if("dog".equals(pet.getType())){
                this.dogQ.add(new DogAndCat(pet, this.count++));
            }else if("cat".equals(pet.getType())){
                this.catQ.add(new DogAndCat(pet, this.count++));
            }else{
                throw new RuntimeException("error, not dog or cat");
            }
        }
        public Pet pollAll(){
            if(!this.dogQ.isEmpty() && !this.catQ.isEmpty()){
                if(this.dogQ.peek().getCount() < this.catQ.peek().getCount()){
                    return this.dogQ.poll().getPet();
                }else{
                    return this.catQ.poll().getPet();
                }
            }else if(!this.dogQ.isEmpty()){
                return this.dogQ.poll().getPet();
            }else if(!this.catQ.isEmpty()){
                return this.catQ.poll().getPet();
            }else{
                throw new RuntimeException("error, queue is empty");
            }
        }

        public Dog pollDot(){
            if(!this.isDogQueueEmpty()){
                return (Dog) this.dogQ.poll().getPet();
            }else{
                throw new RuntimeException("dog queue is empty");
            }
        }

        public Cat pollCat(){
            if(!this.isCatQueueEmpty()){
                return (Cat) this.catQ.poll().getPet();
            }else{
                throw new RuntimeException("dog queue is empty");
            }
        }

        public boolean isEmpty(){
            return this.dogQ.isEmpty() && this.catQ.isEmpty();
        }

        public boolean isDogQueueEmpty(){
            return this.dogQ.isEmpty();
        }

        public boolean isCatQueueEmpty(){
            return this.catQ.isEmpty();
        }
    }

    private static class Pet{
        private String type;
        public Pet(String type){
            this.type = type;
        }
        public String getType(){
            return type;
        }
    }
    private static class Dog extends Pet{
        public Dog(String type) {
            super("dot");
        }
    }
    private static class Cat extends Pet{
        public Cat(String type){
            super("cat");
        }
    }
}


